package com.cuitrip.component.para;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

import com.cuitrip.app.MainApplication;
import com.cuitrip.component.para.model.Para;
import com.cuitrip.component.para.ui.ParaEditHolderView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.adapter.BaseHolderView;
import com.lab.adapter.HolderViewAdapter;
import com.lab.component.list.LinearListView;
import com.lab.utils.ArrayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ak on 15/10/19.
 */
public class ParaListEditLayout extends LinearListView implements ParaEditHolderView.EditTextCallBack {

    private HolderViewAdapter mAdapter;
    private List<Para> paraList = new ArrayList<>();
    private int maxLimit = 200;
    private int requestFocusPosition = 0;
    private TextView textView;

    public ParaListEditLayout(Context context) {
        super(context);
        initInternal();
    }

    public ParaListEditLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initInternal();
    }

    private void initInternal() {
        mAdapter = new HolderViewAdapter(MainApplication.getInstance(), paraList, ParaEditHolderView.class);
        mAdapter.setHolderViewCallback(new HolderViewAdapter.HolderViewCallback() {
            @Override
            public void onHolderViewInvalidate(BaseHolderView holderView, int position) {
                if (holderView instanceof ParaEditHolderView) {
                    ParaEditHolderView editTextHolderView = (ParaEditHolderView) holderView;
                    editTextHolderView.setEditTextCallBack(ParaListEditLayout.this);
                    if (position == requestFocusPosition) {
                        editTextHolderView.requestFocus();
                        editTextHolderView.checkSelection();
                    }
                }
            }
        });
        setAdapter(mAdapter);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                addPara(0);
                textView = new TextView(getContext());
                textView.setTextColor(ResourceUtil.getColor(R.color.ct_black));
                textView.setText("ttt");
                addView(textView);
            }
        }, 500);
    }

    public void addPara(int position){
        if (position == 0 || ArrayUtil.isIndexSafe(position, paraList)) {
            String content = new String();
            Para para = new Para(content);
            paraList.add(para);
            requestFocusPosition = position + 1;
            mAdapter.notifyDataSetChanged();
        }
    }

    public void removePara(int position){
        if (ArrayUtil.isIndexSafe(position, paraList)) {
            if (paraList.size() == 1) {
                return;
            }
            paraList.remove(position);
            if (position > 0){
                requestFocusPosition = position - 1;
            }else{
                requestFocusPosition = position;
            }
            mAdapter.notifyDataSetChanged();
        }
    }

    public void setParas(List<Para> list) {
        paraList.clear();
        paraList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }


    @Override
    public void onKeyEnter(ParaEditHolderView editTextHolderView, int position) {
        addPara(position);
    }

    @Override
    public void onKeyClear(ParaEditHolderView editTextHolderView, int position) {
        removePara(position);
    }

    @Override
    public boolean onTextNumberChanged(int number) {
        int editCount = getEditCount();
        if (editCount > maxLimit) {
//            setTextCount(editCount);
            return false;
        } else {
//            setTextCount(editCount);
            return true;
        }

    }

    private int getEditCount() {
        int count = 0;
        for (Para para : paraList) {
            count = count + para.getText().length();
        }
        return count;
    }


}
